# -*- coding: utf-8 -*-
import scrapy
from readFund.items import ReadfundItem

class EastmoneySpider(scrapy.Spider):
    name = 'eastmoney'
    allowed_domains = ['fund.eastmoney.com']
    url="http://fund.eastmoney.com/data/rankhandler.aspx?op=ph&dt=kf&ft=all&rs=&gs=0&sc=zzf&st=desc&sd=2017-11-05&ed=2018-11-05&qdii=&tabSubtype=,,,,,&pi=1&pn=10000&dx=1&v=0.15050694382895213"
    start_urls = [url]

    def parse(self, response):
        url="http://fund.eastmoney.com/"
        item=ReadfundItem()
        data = response.text
        data=data.replace('var rankData =','')
        data=data[:-1]
        s=data.find('[')
        e=data.find(']')
        dictdata=data[s+2:e-1]

        listData=dictdata.split('","')

        #print(type(listData[0]))

        for fund in listData:
            d=fund.split(',')
            zqdm=d[0]
            url="http://fund.eastmoney.com/{}.html".format(zqdm)

            item['zqdm'] = zqdm
            item['zqurl']=url
            item['zqmc']=d[1]
            item['fdate']=d[3]
            item['dwjz']=d[4] if d[4]!='' else 0.0
            item['ljjz']=d[5] if d[5]!='' else 0.0
            item['rzzl']=d[6] if d[6]!='' else 0.0
            item['jz1w']=d[7] if d[7]!='' else 0.0
            item['jz1m']=d[8] if d[8]!='' else 0.0
            item['jz3m']=d[9] if d[9]!='' else 0.0
            item['jz6m']=d[10] if d[10]!='' else 0.0
            item['jz1y']=d[11] if d[11]!='' else 0.0
            item['jz2y']=d[12] if d[12]!='' else 0.0
            item['jz3y']=d[13] if d[13]!='' else 0.0
            item['now_year']=d[14] if d[14]!='' else 0.0
            item['established']=d[15] if d[15]!='' else 0.0

            yield item